{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 循环\n",
    "\n",
    "话题：\n",
    "1. `while`循环\n",
    "2. `for`循环\n",
    "<br>\n",
    "\n",
    "## while循环\n",
    "\n",
    "`while`循环语法：\n",
    "\n",
    "```julia\n",
    "while *condition*\n",
    "    *loop body*\n",
    "end\n",
    "```\n",
    "\n",
    "举个例子，可以使用`while`来数数或者遍历一个数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 0\n",
    "while n < 10\n",
    "    n += 1\n",
    "    println(n)\n",
    "end\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "i = 1\n",
    "while i <= length(myfriends)\n",
    "    friend = myfriends[i]\n",
    "    println(\"Hi $friend, it's great to see you!\")\n",
    "    i += 1\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for循环\n",
    "\n",
    "`for`循环语法：\n",
    "\n",
    "```julia\n",
    "for *var* in *loop iterable*\n",
    "    *loop body*\n",
    "end\n",
    "```\n",
    "\n",
    "上面两个例子都可以用for循环改写："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for n in 1:10\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "for friend in myfriends\n",
    "    println(\"Hi $friend, it's great to see you!\")\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "让我们来试试用`for`循环创建加和表，表的每个元素为其行索引和列索引的和。<br>\n",
    "\n",
    "首先，来初始化一个全零的数组（矩阵）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m, n = 5, 5\n",
    "A = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:m\n",
    "    for j in 1:n\n",
    "        A[i, j] = i + j\n",
    "    end\n",
    "end\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "乘机介绍一下像上面这种嵌套`for`循环的语法糖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:m, j in 1:n\n",
    "    B[i, j] = i + j\n",
    "end\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建同样的表但更具有“Julia”风格的方法是使用*数组解析式（array comprehension）*。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = [i + j for i in 1:m, j in 1:n]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习\n",
    "\n",
    "#### 4.1\n",
    "循环遍历1到100的整数并打印她们的平方。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2\n",
    "给上一题的代码添一点以创建一个字典，使字典存着每个整数和它的平方的键值对，就像这样\n",
    "\n",
    "```julia\n",
    "squares[10] == 100\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "3e96c8051261bb7e0da6df69d54fbe89",
     "grade": true,
     "grade_id": "cell-54834a2e4e122b70",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert squares[10] == 100\n",
    "@assert squares[11] == 121"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 4.3\n",
    "用数组解析式来创建一个数组`squares_arr`，让它存着从1到100的整数的平方。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "f0331aaa26a8ec9bc2e637073115083c",
     "grade": true,
     "grade_id": "cell-17d4631c51b8b4f7",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert length(squares_arr) == 100\n",
    "@assert sum(squares_arr) == 338350"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "完成练习后请点击顶部的`Validate`。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
